package cn.kzgg.org.service.impl;

import cn.kzgg.basic.service.impl.BaseServiceImpl;
import cn.kzgg.basic.util.MD5Utils;
import cn.kzgg.basic.util.PageList;
import cn.kzgg.basic.util.StrUtils;
import cn.kzgg.org.domain.Employee;
import cn.kzgg.org.mapper.EmployeeMapper;
import cn.kzgg.org.query.EmployeeQuery;
import cn.kzgg.org.service.IEmployeeService;
import cn.kzgg.user.domain.Logininfo;
import cn.kzgg.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;


    @Override
    public void save(Employee employee) {
        //保存之前休闲改改，初始化
        initEmployee(employee);
        //级联修改logininfo
        Logininfo logininfo = emp2logininfo(employee);
        logininfoMapper.save(logininfo);
        employee.setLogininfo_id(logininfo.getId());
        employeeMapper.save(employee);
    }

    private Logininfo emp2logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        //手动设置员工
        logininfo.setType(0);
        return logininfo;

    }

    private void initEmployee(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
    }


    @Override
    public void update(Employee employee) {
        super.update(employee);
    }

    @Override
    public void removeById(Long id) {
        super.removeById(id);
    }

    /**
     * 根据LogininfoId查到处理人employee绑定店铺
     * @param id
     * @return
     */
    @Override
    public Employee loadByLogininfoId(Long id) {
        return employeeMapper.loadByLogininfoId(id);
    }

    /**
     * 查询店铺的店员
     * @param shopId
     * @return
     */
    @Override
    public List<Employee> queryShopEmployee(Long shopId) {
        return employeeMapper.queryShopEmployee(shopId);
    }
}
